/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package foalvleditor.core;

import foalvleditor.core.impl.LevelModel;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.xml.parsers.ParserConfigurationException;

/**
 *
 * @author Mikael
 */
public class AppFileHandler {

    private String path="";
    private LevelModel levelModel=LevelModel.getInstance();
    private static AppFileHandler instance=new AppFileHandler();

    public static AppFileHandler getInstance(){
        return instance;
    }

    public void setPath(String newPath){
        path=newPath;
    }

    public String getPath(){
        return path;
    }

    public void openFile(){
        JFileChooser jfc=new JFileChooser(path);
        jfc.setFileFilter(new FileDialogFilter(".xml",
                "Force of Attraction Level File (*.xml)"));
        if (jfc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
            path=jfc.getSelectedFile().getAbsolutePath();
            try{
                LevelParser.getInstance().parseLevel(path);
                  if (levelModel.getLevel().isEmpty()) throw new Exception();
                  levelModel.getView().paintCurrentLevel();
                  levelModel.getView().getFrame().setTitle("FoA Level Editor - ["+path+"]");
            }
            catch (Exception e){
                JOptionPane.showMessageDialog (null, "The file is not a valid FoA level file.");
            }
            }
    }

    public void saveFileAs() throws ParserConfigurationException {
        if (levelModel.containsPlayer())
        {
        JFileChooser jfc=new JFileChooser(path);
        jfc.setFileFilter(new FileDialogFilter(".xml",
                "Force of Attraction Level File (*.xml)"));
        if (jfc.showSaveDialog(null)==JFileChooser.APPROVE_OPTION){
            path=jfc.getSelectedFile().getAbsolutePath();
            if (!path.endsWith(".xml")) path+=".xml";
                saveFile();
            levelModel.getView().getFrame().setTitle("FoA Level Editor - ["+path+"]");
         }
        }
        else if (!levelModel.containsPlayer()) JOptionPane.showMessageDialog
                (null, "Unable to save the level.\nNo player character placed.");
    }

   public void saveFile() throws ParserConfigurationException {
        if (levelModel.containsPlayer())
       {
            try{
                FileWriter fw=new FileWriter(path);
                String xmlCode=XMLParser.parseLevel();
                fw.write(xmlCode);
                fw.close();
                levelModel.setChanged(false);

            }
            catch (IOException e){
                 JOptionPane.showMessageDialog (null, "I/O problem while saving the file!");
            }
            catch (CloneNotSupportedException e){
                 JOptionPane.showMessageDialog (null, "Internal error");
            }
        }
        else if (!levelModel.containsPlayer()) JOptionPane.showMessageDialog
                (null, "Unable to save the level.\nNo player character placed.");
    }


}
